package com.dldata.devtools30.controller.zhibiao;

import com.dldata.devtools30.entity.DevtZbplanInfoEntity;
import com.dldata.devtools30.entity.DevtZbplanTypeEntity;
import com.dldata.devtools30.entity.DevtZbplanTypeEntity;
import com.dldata.devtools30.repository.zhibiao.DevtZbplanTypeEntityDao;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping(value = "/DevtZbplanTypeEntityController")
@Slf4j
public class DevtZbplanTypeEntityController {

    @Autowired
    DevtZbplanTypeEntityDao devtZbplanTypeEntityDao;
    
    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Result getAll(@RequestParam Map<String, String> paramMap){
        
        List<DevtZbplanTypeEntity> typeEntityArrayList =Lists.newArrayList(devtZbplanTypeEntityDao.findAll());
        typeEntityArrayList.sort(new Comparator<DevtZbplanTypeEntity>() {
            @Override
            public int compare(DevtZbplanTypeEntity o1, DevtZbplanTypeEntity o2) {
                if(o1.getXh()==null) o1.setXh(0);
                if(o2.getXh()==null) o2.setXh(0);
                return o1.getXh().compareTo(o2.getXh());
            }
        });
        return Result.ok(typeEntityArrayList);
    }
    
    
    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(DevtZbplanTypeEntity entity){
        if(StringUtil.isEmptyOrLength0(entity.getTypename())){
            return Result.error("名称不能为空");
        }
        
        if(StringUtil.isEmptyOrLength0(entity.getId())){
            entity.setId(UUID.randomUUID().toString());
        }
        devtZbplanTypeEntityDao.save(entity);
        
        return Result.ok();
    }
    
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam Map<String, String> paramMap){
        
        String id =paramMap.get("id");
        if(StringUtil.isEmptyOrLength0(id)){
            return  Result.error("参数有误");
        }
        try {
            DevtZbplanTypeEntity devtZbclassInfoEntity = devtZbplanTypeEntityDao.findById(id).orElse(null);
            if(devtZbclassInfoEntity!=null){
                devtZbplanTypeEntityDao.delete(devtZbclassInfoEntity);
            }
        }
        catch (Exception ex){
            log.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage(),ex);
            return  Result.error("操作错误，请稍候再试!");
        }
        return  Result.ok("删除成功");
    }
    
    @RequestMapping("edit")
    public String toEdit(String id, Model model){
        DevtZbplanTypeEntity zbobjInfoEntity = new DevtZbplanTypeEntity();
        if(!StringUtil.isEmptyOrLength0(id)){
            zbobjInfoEntity = devtZbplanTypeEntityDao.findById(id).orElse(new DevtZbplanTypeEntity());
        }
        model.addAttribute("entity",zbobjInfoEntity);
        
        return "thymeleaf/devtools30/zhibiao/v_zhibiaoplantypeEdit";
    }
    
    
    @RequestMapping("toList")
    public String toList(Model model){
        
        return "thymeleaf/devtools30/zhibiao/v_zhibiaoplantypeList";
    }
    
}
